package com.ml.nio.zerocopy;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * NIO 零拷贝案例 - 服务器
 * 案例要求：
 * 1) 使用传统的IO 方法传递一个大文件
 * 2) 使用NIO 零拷贝方式传递(transferTo，transferTo 底层使用到零拷贝)一个大文件
 * 3) 看看两种传递方式耗时时间分别是多少
 */
public class NewIOServer {

    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(7001);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(address);
        //创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readcount = 0;
            while (-1 != readcount) {
                try {
                    readcount = socketChannel.read(byteBuffer);
                }catch (Exception ex) {
                    break;
                }
                byteBuffer.rewind(); //倒带 position = 0, mark 作废。
            }
        }
    }
}
